<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SitioController
 *
 * @author Nayeli
 */
class ShipmentController extends AppController
{
    public function beforeFilter()
    {
        parent::beforeFilter();
    }

    public function index()
    {
        $this->set('title_for_layout', 'Envios Disponibles'); 
        $this->layout = 'base';
        
        $limit=10;
        $this->Shipment->shipmentsList($limit);
      /*
       foreach ($sLimit as $value)
        {
           if ($value['zone']['Zone_Name']== NULL)
           { $zone='<td>'.'Sin Zona<br /></td>'; }
           else 
           { $zone='<td>'.$value['zone']['Zone_Name'].' ('.$value['zone']['Zone_Number'].')<br /></td>'; }
           
           $this->View->shipmentsDetails.='<tr><td>'.$value['shipments']['Shipments_Id'].'<br /></td>'
                   .'<td>'.$value['citiesOrigin']['City_Origin_Name'].' , '.$value['statesOrigin']['State_O_Abbrev'].' - '.$value['cities']['City_Destiny_Name'].' , '.$value['states']['State_D_Abbrev'].'<br /></td>'
                   .'<td>'.$value['vehicles']['Vehicle_Name'].' De '.$value['vehicle_types']['Vehicle_Type_Name'].'<br /></td>'
                   .$zone.'<td>'.$value['0']['New_Availability_Date'].'<br /></td></tr>';
        }
       */
       $this->set('shipmentsDetails', $this->Shipment->shipmentsList($limit));
       
       $imagen = 'banner/masfletes_2.jpg';
       $altima = 'Masfletes_Servicios';
       $this->set('imagen',$imagen);
       $this->set('altima',$altima);
    }
    
    public function publicaCargaExpress() {
        $this->set('title_for_layout', 'Publica tu carga gratis'); 
        $this->layout = 'base';
       
        $this->loadModel("State");
       $this->set('estados', $this->parseForSend($this->State->find('all', array(
           'fields' => array('id', 'name')
           ))));
       $this->loadModel("Vehicle");
       $this->set('vehiculos', $this->parseForSend($this->Vehicle->find('all', array(
           'fields' => array('id', 'name')
           ))));
       $this->loadModel("VehicleType");
       $this->set('tVehiculos', $this->parseForSend($this->VehicleType->find('all', array(
           'fields' => array('id', 'name')
           ))));
       $this->loadModel("ShipmentTypes");
       $this->set('cargas', $this->parseForSend($this->ShipmentTypes->find('all', array(
           'fields' => array('id', 'name')
           ))));
       
       if($this->request->is("post"))
       {
            $datos = $this->request->data;
            $usuario = array();
            $usuario['username'] = $datos['correo_registrarse'];
            $usuario['type'] = 3;
            $this->loadModel("User");
            try
            {
                $var = $this->User->save($usuario);
                $shipment = array();
                $shipment['user_id'] = $var['User']['id'];
                $shipment['creation_date'] = date("Y-m-d H:i:s");
                $shipment['effective_days'] = $datos['vigencia'];
                $shipment['source_date'] = $datos['disponibilidad'];
                $shipment['vehicle_id'] = $datos['vehiculo'];
                $shipment['vehicle_type_id'] = $datos['tipoVehiculo'];
                $shipment['state_origin_id'] = $datos['eOrigen'];
                $shipment['state_destiny_id'] = $datos['eDestino'];
                $shipment['municipality_origin_id'] = $datos['mOrigen'];
                $shipment['municipality_destiny_id'] = $datos['mDestino'];
                $shipment['comments'] = $datos['comentarios'];
                $shipment['shipment_type_id'] = $datos['tipoCarga'];
                try
                {
                    $this->loadModel('Shipment');
                    $this->Shipment->save($shipment);
                    $this->set('rerror', false);
                }
                catch (PDOException $e)
                {
                    $this->set('rerror', true);
                }
            }
            catch (PDOException $e)
            {
                $this->set('rerror', true);
            }
       }
       
     }
     
     public function dameMunicipios()
    {
        $this->loadModel("Citie");
        $municipios = $this->parseForSend($this->Citie->find('all', array(
            'conditions' => array("state_id" => $this->request->data['id']),
            'fields' => array('id', 'name')
        )));
        $this->set(array('municipios'=>$municipios, '_serialize'=>'municipios'));
        $this->viewClass = 'Json';
    }
    
     private function parseForSend($array)
    {
        $nArray = array();
        
        foreach ($array as $value) {
            $var = each($value);
            $nArray[$var[1]['id']] = $var[1]['name'];
        }
        
        return $nArray;
    }
}

?>
